package cn.edu.lsnu.mbti.controller;

import cn.edu.lsnu.mbti.common.R;
import cn.edu.lsnu.mbti.dto.UserDto;
import cn.edu.lsnu.mbti.entity.User;
import cn.edu.lsnu.mbti.entity.vo.UserVo;
import cn.edu.lsnu.mbti.exception.CustomerException;
import cn.edu.lsnu.mbti.service.UserService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;


    /**
     * vue后台管理修改用户状态
     * @param user
     * @return
     */
    @PutMapping("updateStatus")
    public R updateStatus(@RequestBody User user){
        userService.updateUserStatus(user);
        return R.success(null);
    }


    /**
     * vue后台管理删除用户
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public R<String> delete(@PathVariable("id") String id){
        if (StrUtil.isBlank(id)){
            throw new CustomerException("无法删除空对象");
        }
        return R.success(userService.deleteById(id));
    }


    /**
     * vue后台管理修改用户
     * @return
     */
    @PostMapping("saveUser")
    public R<UserDto> saveUser(@RequestBody User user){
        if (BeanUtil.isEmpty(user)){
            throw new CustomerException("不能保存空对象");
        }
        return R.success(userService.saveUser(user));
    }


    /**
     * 用户做完mbti测试 根据id修改用户的人格数据接口
     * @param userDto
     * @return
     */
    @PutMapping("addUserPersonality")
    public R<UserDto> addUserPersonality(@RequestBody UserDto userDto){
        return R.success(userService.addUserPersonality(userDto));
    }


    /**
     * vue测试渲染表格
     * @return
     */
    @PostMapping("list")
    public R<Page<UserDto>> list(@RequestBody UserVo userVo){
        if (BeanUtil.isEmpty(userVo)){
            throw new CustomerException("不能查询空对象");
        }
        return R.success(userService.pageList(userVo));
    }


    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PutMapping("/edit")
    public R<String> edit(@RequestBody User user){
        return userService.edit(user);
    }

    /**
     * 完善个人信息（回显数据）
     * @param id
     * @return
     */
    @GetMapping("/selectForEdit")
    public R<UserDto> selectForEdit(@RequestParam String id){
        return R.success(userService.selectForEdit(id));
    }


    /**
     * 根据id查询用户详细信息
     * @param id
     * @return
     */
    @GetMapping("/getOneById")
    public R<UserDto> getOneById(@RequestParam String id){
        return R.success(userService.selectForEdit(id));
    }

    /**
     * 登出
     * @return
     */
    @GetMapping("/logout")
    public R<String> logout(@RequestParam String id){
        return userService.logout(id);
    }

    /**
     * 注册
     * @param user
     * @return
     */
    @PostMapping("register")
    public R<User> register(@RequestBody User user){
        return userService.register(user);
    }

    /**
     * 注册时根据用户名查询用户
     * @param user
     * @return
     */
    @GetMapping("/getOneByUsername")
    public R<String> getOneByUsername(@RequestBody User user){
        return userService.getOneByUsername(user);
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping("/login")
    public R<UserDto> login(@RequestBody User user){
        return R.success(userService.login(user));
    }
}
